import { Tools } from 'weaver-mobile';

const escape2Html = str => {
  var arrEntities = { lt: "<", gt: ">", nbsp: " ", amp: "&", quot: '"' };
  return str.replace(/&(lt|gt|nbsp|amp|quot);/gi, function(all, t) {
    return arrEntities[t];
  });
};

export { escape2Html };


export default {
  addWindowFunc: () => {
      window.openAppLink = (obj, linkid) => {

          let linkType = obj.attributes.linktype.nodeValue;
          if (linkType == "doc")
              window.openLink.openDocument(linkid);
          else if (linkType == "task")
            window.openLink("/spa/prj/static4mobile/index.html#/TaskDetails/0/"+linkid);
          else if (linkType == "crm")
              window.openLink("/spa/crm/static4mobile/index.html/#/customerCard/"+linkid);
          else if (linkType == "workflow")
              window.openLink("/spa/workflow/static4mobileform/index.html#/req?requestid="+linkid);
          else if (linkType == "project")
              window.openLink("/spa/prj/static4mobile/index.html#/ProjectDetails/0/"+linkid);
          else if (linkType == "workplan")
              window.openLink.openWorkplan(linkid);
          return false;
      }

      window.opendoc1 = (showid) => {
          // let id =  searchPNode(obj,obj.parentNode.id);
          // let options = {
          //     method: 'POST',
          //     url: '/api/blog/attach/AddBlogDocShare',
          //     params: {
          //         jsonStr:JSON.stringify({
          //             "blogid":id,
          //             "docid":showid
          //         })}
          // };
          //   Tools.callApi(options).then((result)=>{
          //     if(result.status=="1"){
          //         window.openLink.openDocument(showid,{blogDiscussid:id});
          //     }
          // });
          window.openLink.openDocument(showid);
      }
      window.downloads = (fileid) => {
        //let id =  searchPNode(obj,obj.parentNode.id); 
        //   let options = {
        //       method: 'POST',
        //       url: '/api/blog/attach/AddBlogDocShare',
        //       params: {
        //           jsonStr:JSON.stringify({
        //               "blogid":id,
        //               "docid":files
        //           })}
        //   };
        //    Tools.callApi(options).then((result)=>{
        //      if(result.status=="1"){
        //          window.openLink.getWeaverFile("/weaver/weaver.file.FileDownload?fileid=" + files + "&download=1&blogDiscussid="+id)
        //      }
        //  });
        // window.openLink.getWeaverFile("/weaver/weaver.file.FileDownload?fileid=" + fileid + "&download=1")
        Tools.getWeaverFile("/weaver/weaver.file.FileDownload?fileid=" + fileid + "&download=1",'window')
      }
  }

};
//递归获取父节点
function searchPNode(obj,value) {
  if(value=="wm-comment"){
      return obj.id;
  }else {
      return searchPNode(obj.parentNode,obj.parentNode.className);
  }

}